import 'package:flutter_menu/biz/order/detail/option/options_controller.dart';
import 'package:flutter_menu/model/product_model.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:get/get.dart';

import 'option_item.dart';

// 规格选择组件
class SpecSelector extends GetView<OptionsController> {
  const SpecSelector({super.key, this.title, required this.specList});

  final String? title;
  final List<Spec> specList;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OptionsController>(
      id: OptionsController.idSpecWidget,
      init: OptionsController(),
      builder: (OptionsController controller) {
        debugPrint("SpecSelector Build");
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 规格选择
            Text(title ?? "",
                style:
                    TextStyle(fontSize: 26.sp, color: AppColors.text_CA7A2A)),
            SizedBox(height: 20.w),
            specList
                .map((item) {
                  return buildSpecItem(item, controller);
                })
                .toList()
                .toWrap(
                  spacing: 25.0.w, // 水平方向的间距
                  runSpacing: 16.0.w, // 垂直方向的间距
                ),
          ],
        );
      },
    );
  }

  Widget buildSpecItem(Spec item, OptionsController controller) {
    var name = item.name ?? "";
    var id = item.id ?? "";
    var selectedSpecId = controller.selectedSpecId;
    return OptionItem(
      label: name,
      isEnable: true,
      isSelected: selectedSpecId == id,
      onTap: () {
        controller.updateSelectedSpecId(id);
      },
    );
  }
}
